<template>
  <el-form ref="form" label-width="80px">
    <el-form-item label="SPU名称">{{ spuName }}</el-form-item>

    <el-form-item label="SKU名称">
      <el-input v-model="skuInfo.skuName" placeholder="SKU名称"></el-input>
    </el-form-item>

    <el-form-item label="价格(元)">
      <el-input v-model="skuInfo.price" type="number" placeholder="价格(元)"></el-input>
    </el-form-item>

    <el-form-item label="重量(千克)">
      <el-input v-model="skuInfo.weight" placeholder="重量(千克)"></el-input>
    </el-form-item>

    <el-form-item label="规格描述">
      <el-input v-model="skuInfo.skuDesc" type="textarea" :rows="5" placeholder="请输入内容"></el-input>
    </el-form-item>

    <el-form-item label="平台属性">
      <el-form label-width="80px" :inline="true">
        <el-form-item v-for="attr in attrInfoList" :key="attr.id" :label="attr.attrName" style="padding:0 0 10px 0">
          <el-select v-model="attr.attrValue" placeholder="请选择">
            <!-- v-for="item in options" :key="item.value" :label="item.label" :value="item.value" -->
            <el-option
              v-for="attrValue in attr.attrValueList"
              :key="attrValue.id"
              :label="attrValue.valueName"
              :value="attrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>

    <el-form-item label="销售属性">
      <el-form label-width="80px" :inline="true">
        <el-form-item
          v-for="spuSaleAttr in spuSaleAttrList"
          :key="spuSaleAttr.id"
          :label="spuSaleAttr.saleAttrName"
          style="padding:0 0 10px 0"
        >
          <el-select v-model="spuSaleAttr.saleAttrValueId" placeholder="请选择">
            <!-- v-for="item in options" :key="item.value" :label="item.label" :value="item.value" -->
            <el-option
              v-for="spuSaleAttrValue in spuSaleAttr.spuSaleAttrValueList"
              :label="spuSaleAttrValue.saleAttrValueName"
              :key="spuSaleAttrValue.id"
              :value="spuSaleAttrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>

    <el-form-item label="图片列表">
      <el-table :data="spuImageList" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column prop="prop" label="图片">
          <!-- 作用域插槽 -->
          <template v-slot="{row,$index}">
            <img :src="row.imgUrl" style="width:100px;height:100px" alt />
          </template>
        </el-table-column>
        <el-table-column prop="imgName" label="名称"></el-table-column>
        <el-table-column prop="prop" label="操作">
          <!-- 作用域插槽 -->
          <template v-slot="{row,$index}">
            <el-button
              v-if="row.isDefault === 0"
              type="primary"
              icon="el-icon-edit-outline"
              @click="changeDefault(row)"
            >设为默认</el-button>
            <el-button v-else type="success" plain icon="el-icon-check">默认</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'SkuForm',
  data () {
    return {
      attrInfoList: [],
      spuSaleAttrList: [],
      spuImageList: [],
      spuName: '',
      // 点击保存时提交的sku信息
      skuInfo: {
        // 从父组件获得的数据
        category3Id: '',
        spuId: '',
        // 品牌id
        tmId: '',
        // 组件中收集的数据
        skuName: '',
        // 价格
        price: '',
        // 重量
        weight: '',
        // 描述
        skuDesc: '',
        // 默认图片url
        skuDefaultImg: '',
        // 平台属性
        skuAttrValueList: [],
        // 销售属性
        skuSaleAttrValueList: [],
        // 图片列表
        skuImageList: []
      },
      // 收集被勾选的图片信息，将来提交给服务器时，需要整理参数并添加isDefault
      imageList: []
    }
  },
  methods: {
    // 获取到spu主页传来的skuForm所需数据
    async getData (category1Id, category2Id, spu) {
      // 获取spu父组件中的数据
      // console.log(spu)
      this.skuInfo.category3Id = spu.category3Id
      this.skuInfo.spuId = spu.id
      this.skuInfo.tmId = spu.tmId
      this.spuName = spu.spuName

      // 获取平台属性
      const attrInfoResult = await this.$api.spu.reqAttrInfoList(category1Id, category2Id, spu.category3Id)
      // console.log(result)
      if (attrInfoResult.code === 200) {
        this.attrInfoList = attrInfoResult.data
      }

      // 获取销售属性
      const spuSaleAttrResult = await this.$api.spu.reqSpuSaleAttrList(spu.id)
      // console.log(supSaleAttrResult)
      if (spuSaleAttrResult.code === 200) {
        this.spuSaleAttrList = spuSaleAttrResult.data
      }

      // 获取图片墙
      const spuImageResult = await this.$api.spu.reqSpuImageList(spu.id)
      // console.log(spuImageResult)
      if (spuImageResult.code === 200) {
        // 给获取到的每张图片对象中添加isDefault属性
        const list = spuImageResult.data
        list.forEach(item => {
          item.isDefault = 0
        })
        this.spuImageList = list
      }
    },

    // 取消按钮回调（回到spu主页）
    cancel () {
      this.$emit('changeScene', { scene: 0, flag: '' })
      // 返回spu主页后清除skuForm列表
      Object.assign(this._data, this.$options.data())
    },

    // 选中图片列表中某张图片的回调
    handleSelectionChange (value) {
      // console.log(value)
      // 获取到用户选中的图片信息，获取的信息需要补上isDefault属性，判断是否是默认图片
      this.imageList = value
      // console.log(this.imageList)
    },

    // 点击后【设置默认】后回调
    changeDefault (row) {
      // 排他操作，把之前选中的默认全部重置
      this.spuImageList.forEach(item => {
        item.isDefault = 0
      })
      row.isDefault = 1
      // 将默认图片的url收集到skuInfo中
      this.skuInfo.skuDefaultImg = row.imgUrl
    },

    // 保存新添加的sku
    async save () {
      // 整理参数
      const { skuInfo, attrInfoList, spuSaleAttrList, imageList } = this

      // 整理平台属性
      attrInfoList.forEach(item => {
        // 存在attrValue说明平台属性已经选择
        if (item.attrValue) {
          const obj = {
            attrId: item.id,
            valueId: item.attrValue
          }
          skuInfo.skuAttrValueList.push(obj)
        }
      })

      // 整理销售属性
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.saleAttrValueId) {
          const obj = {
            saleAttrId: item.id,
            saleAttrValueId: item.saleAttrValueId
          }
          prev.push(obj)
        }
        return prev
      }, [])

      // 整理图片属性
      skuInfo.skuImageList = imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id
        }
      })
      const result = await this.$api.spu.reqSaveSkuInfo(skuInfo)
      if (result.code === 200) {
        this.$message.success('添加sku成功')
        // 回到首页
        this.$emit('changeScene', { scene: 0, flag: '' })
      }
    }
  }
}
</script>

<style>
</style>
